package windows;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

public class CharInput extends JDialog implements ActionListener,FocusListener {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private List<JButton> buttons;
	private static char returnValue = ' ';

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		try {
//			CharInput dialog = new CharInput(0);
//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	/**
	 * Create the dialog.
	 */
	public CharInput(int language) {
		setModal(true);
		setBounds(100, 100, 465, 141);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JButton btnHunA = new JButton("\u00C1");
		JButton btnHunE = new JButton("\u00C9");
		JButton btnHunI = new JButton("\u00CD");
		JButton btnHunO1 = new JButton("\u00D3");
		JButton btnHunO2 = new JButton("\u00D6");
		JButton btnHunO3 = new JButton("\u0150");
		JButton btnHunU1 = new JButton("\u00DA");
		JButton btnHunU2 = new JButton("\u00DC");
		JButton btnHunU3 = new JButton("\u0170");
		JButton btnJoker = new JButton("*");
		
		buttons = new ArrayList<>();
		buttons.add(btnHunA);
		buttons.add(btnHunE);
		buttons.add(btnHunI);
		buttons.add(btnHunO1);
		buttons.add(btnHunO2);
		buttons.add(btnHunO3);
		buttons.add(btnHunU1);
		buttons.add(btnHunU2);
		buttons.add(btnHunU3);
		buttons.add(btnJoker);
		for (JButton b : buttons) {
			b.addActionListener(this);
		}
		
		textField = new JTextField();
		textField.addFocusListener(this);
		textField.setColumns(10);
		textField.requestFocus();
		JLabel lblNewCharacter = new JLabel("New character:");
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
					.addComponent(btnHunA)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(btnHunE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnHunI))
						.addComponent(lblNewCharacter))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
							.addComponent(textField)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnJoker))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(btnHunO1)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnHunO2)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnHunO3)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnHunU1)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnHunU3)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnHunU2)
					.addGap(21))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnHunA)
						.addComponent(btnHunE)
						.addComponent(btnHunI)
						.addComponent(btnHunO1)
						.addComponent(btnHunO2)
						.addComponent(btnHunO3)
						.addComponent(btnHunU1)
						.addComponent(btnHunU3)
						.addComponent(btnHunU2))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewCharacter)
						.addComponent(btnJoker)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(13, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if (returnValue != ' ') {
							setVisible(false);
							dispose();
						} else {
							JOptionPane.showMessageDialog(contentPanel,
								    "Must Input a character",
								    "Error",
								    JOptionPane.ERROR_MESSAGE);
//							if (((returnValue >= 'a') && (returnValue <= 'z')) || ((returnValue >= 'A') && (returnValue <= 'Z'))) {
//								
//							}
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						returnValue = ' ';
						setVisible(false);
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		setVisible(true);
	}
	
	public static char chooseCharacter(int language) {
		returnValue = ' ';
		new CharInput(language);
		return Character.toUpperCase(returnValue);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		int i = 0;
		while (i < buttons.size()) {
			if (buttons.get(i) == e.getSource()) {
				break;
			}
			i++;
		}
		System.out.println(i);
		textField.setText(buttons.get(i).getText());
		if (textField.getText() != null) {
			returnValue = textField.getText().charAt(0);
		}
		
	}

	@Override
	public void focusGained(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		System.out.println("focus lost");
		if (textField.getText() == "") {
			returnValue = ' ';
		} else {
			returnValue = textField.getText().charAt(0);
		}
		
	}

}
